rm(list=ls())

library(tidyverse)
library(ggplot2)
theme_set(theme_bw())
library(descr)
library(readxl)
library(readr)
library(estimatr)
library(scales)
library(texreg)
library(ggpubr)

setwd("")
data <- read.csv("main_data.csv")

#Preparing the treatment variable: if randomly assigned to listen to male candidates=1, female candidates=0
data$treatment <- NA
data$treatment[data$female_candidate==1]<-0
data$treatment[data$female_candidate==0]<-1

data$resp_gender_num <- 0
data$resp_gender_num[data$gender_identity==1] <- 1

##Figure 2 in the manuscript
lm1 <- lm_robust(scale10 ~ treatment, data=data) #0 difference between LP and HP
summary(lm1)
coefficients_10<-lm_robust(scale10 ~ treatment, data=data)$coefficients
ses_10<-(lm_robust(scale10 ~ treatment, data=data)$std.error)*100

lm2 <- lm_robust(scale98 ~ treatment, data=data) #200 difference between LP and HP
summary(lm2)
coefficients_98<-lm_robust(scale98 ~ treatment, data=data)$coefficients
ses_98<-(lm_robust(scale98 ~ treatment, data=data)$std.error)*100

lm3 <- lm_robust(scale96 ~ treatment, data=data) #400 difference between LP and HP
summary(lm3)
coefficients_96<-lm_robust(scale96 ~ treatment, data=data)$coefficients
ses_96<-(lm_robust(scale96 ~ treatment, data=data)$std.error)*100

lm4 <- lm_robust(scale94 ~ treatment, data=data) #600 difference between LP and HP
summary(lm4)
coefficients_94<-lm_robust(scale94 ~ treatment, data=data)$coefficients
ses_94<-(lm_robust(scale94 ~ treatment, data=data)$std.error)*100

lm5 <- lm_robust(scale92 ~ treatment, data=data) #1000 difference between LP and HP
summary(lm5)
coefficients_92<-lm_robust(scale92 ~ treatment, data=data)$coefficients
ses_92<-(lm_robust(scale92 ~ treatment, data=data)$std.error)*100

lm6 <- lm_robust(scale90 ~ treatment, data=data)
summary(lm6)
coefficients_90<-lm_robust(scale90 ~ treatment, data=data)$coefficients
ses_90<-(lm_robust(scale90 ~ treatment, data=data)$std.error)*100

tab0<-NA

tab0 <- data.frame(c(rep("0 TL",1), rep("0 TL",1),rep("200 TL",1),rep("200 TL",1),rep("400 TL",1),rep("400 TL",1),rep("600 TL",1),rep("600 TL",1),rep("800 TL",1),rep("800 TL",1),rep("1000 TL",1),rep("1000 TL",1)))
tab0$Policy <- NA
tab0$Condition <- NA
tab0$mean <- NA
tab0$Cilower <- NA
tab0$Ciupper <- NA
colnames(tab0) <- c("Policy", "Condition", "Mean", "Ci_lower", "Ci_upper")

tab0[1,3]<-coefficients_10[1]*100
tab0[2,3]<-(coefficients_10[2]+coefficients_10[1])*100
tab0[3,3]<-coefficients_98[1]*100
tab0[4,3]<-(coefficients_98[2]+coefficients_98[1])*100
tab0[5,3]<-coefficients_96[1]*100
tab0[6,3]<-(coefficients_96[2]+coefficients_96[1])*100
tab0[7,3]<-coefficients_94[1]*100
tab0[8,3]<-(coefficients_94[2]+coefficients_94[1])*100
tab0[9,3]<-coefficients_92[1]*100
tab0[10,3]<-(coefficients_92[2]+coefficients_92[1])*100
tab0[11,3]<-coefficients_90[1]*100
tab0[12,3]<-(coefficients_90[2]+coefficients_90[1])*100

tab0$Ci_upper<- c(coefficients_10[1]*100 + 1.96*ses_10[1], (coefficients_10[2]+coefficients_10[1])*100 + 1.96*ses_10[2],
                  coefficients_98[1]*100 + 1.96*ses_98[1], (coefficients_98[2]+coefficients_98[1])*100 + 1.96*ses_98[2],
                  coefficients_96[1]*100 + 1.96*ses_96[1], (coefficients_96[2]+coefficients_96[1])*100 + 1.96*ses_96[2],
                  coefficients_94[1]*100 + 1.96*ses_94[1], (coefficients_94[2]+coefficients_94[1])*100 + 1.96*ses_94[2],
                  coefficients_92[1]*100 + 1.96*ses_92[1], (coefficients_92[2]+coefficients_92[1])*100 + 1.96*ses_92[2],
                  coefficients_90[1]*100 + 1.96*ses_90[1], (coefficients_90[2]+coefficients_90[1])*100 + 1.96*ses_90[2])
tab0$Ci_lower<- c(coefficients_10[1]*100 - 1.96*ses_10[1], (coefficients_10[2]+coefficients_10[1])*100 - 1.96*ses_10[2],
                  coefficients_98[1]*100 - 1.96*ses_98[1], (coefficients_98[2]+coefficients_98[1])*100 - 1.96*ses_98[2],
                  coefficients_96[1]*100 - 1.96*ses_96[1], (coefficients_96[2]+coefficients_96[1])*100 - 1.96*ses_96[2],
                  coefficients_94[1]*100 - 1.96*ses_94[1], (coefficients_94[2]+coefficients_94[1])*100 - 1.96*ses_94[2],
                  coefficients_92[1]*100 - 1.96*ses_92[1], (coefficients_92[2]+coefficients_92[1])*100 - 1.96*ses_92[2],
                  coefficients_90[1]*100 - 1.96*ses_90[1], (coefficients_90[2]+coefficients_90[1])*100 - 1.96*ses_90[2])

tab0[1,2]<-"Women Candidates"
tab0[2,2]<-"Men Candidates"
tab0[3,2]<-"Women Candidates"
tab0[4,2]<-"Men Candidates"
tab0[5,2]<-"Women Candidates"
tab0[6,2]<-"Men Candidates"
tab0[7,2]<-"Women Candidates"
tab0[8,2]<-"Men Candidates"
tab0[9,2]<-"Women Candidates"
tab0[10,2]<-"Men Candidates"
tab0[11,2]<-"Women Candidates"
tab0[12,2]<-"Men Candidates"

print(tab0)

Figure2 <- ggplot(tab0, aes(factor(Policy, level = c('0 TL', '200 TL', '400 TL', '600 TL', '800 TL', '1000 TL')), y=Mean, colour=Condition)) + 
  geom_errorbar(aes(ymin=Ci_lower, ymax=Ci_upper),width=.07, position=position_dodge(width=0.5)) +  scale_colour_manual(values=c("tomato3", "turquoise4")) +
  geom_line() + geom_point(aes(shape=Condition),position=position_dodge(width=0.5)) + geom_hline(yintercept=50, colour="grey", linetype = "dashed", linewidth = 1) +
  theme_bw() + coord_cartesian(ylim = c(0, 100)) +  ylab("% voting for the LP candidate") + xlab("Additional public spending an HP candidate offers over an LP candidate") 
print(Figure2)
#ggsave("Figure2.png", dpi=600)

##Figure 3 in the manuscript

coef10<-lm_robust(scale10 ~ treatment, data=data)$coefficients
cilow10<-lm_robust(scale10 ~ treatment, data=data)$conf.low
cihigh10<-lm_robust(scale10 ~ treatment, data=data)$conf.high

coef98<-lm_robust(scale98 ~ treatment, data=data)$coefficients
cilow98<-lm_robust(scale98 ~ treatment, data=data)$conf.low
cihigh98<-lm_robust(scale98 ~ treatment, data=data)$conf.high

coef96<-lm_robust(scale96 ~ treatment, data=data)$coefficients
cilow96<-lm_robust(scale96 ~ treatment, data=data)$conf.low
cihigh96<-lm_robust(scale96 ~ treatment, data=data)$conf.high

coef94<-lm_robust(scale94 ~ treatment, data=data)$coefficients
cilow94<-lm_robust(scale94 ~ treatment, data=data)$conf.low
cihigh94<-lm_robust(scale94 ~ treatment, data=data)$conf.high

coef92<-lm_robust(scale92 ~ treatment, data=data)$coefficients
cilow92<-lm_robust(scale92 ~ treatment, data=data)$conf.low
cihigh92<-lm_robust(scale92 ~ treatment, data=data)$conf.high

coef90<-lm_robust(scale90 ~ treatment, data=data)$coefficients
cilow90<-lm_robust(scale90 ~ treatment, data=data)$conf.low
cihigh90<-lm_robust(scale90 ~ treatment, data=data)$conf.high

tab1<-NA
tab1 <- data.frame(c(rep("0 TL",1), rep("200 TL",1), rep("400 TL",1), rep("600 TL",1), rep("800 TL",1), rep("1000 TL",1)))
tab1$Policy <- NA
tab1$ITT<- NA
tab1$Cilower <- NA
tab1$Ciupper <- NA
colnames(tab1) <- c("Policy", "ITT","Ci_lower", "Ci_upper")
tab1$ITT<- c(coef10[2], coef98[2], coef96[2], coef94[2],coef92[2],coef90[2])*100
tab1$Ci_upper<- c(cihigh10[2], cihigh98[2], cihigh96[2], cihigh94[2], cihigh92[2], cihigh90[2])*100
tab1$Ci_lower<- c(cilow10[2], cilow98[2], cilow96[2], cilow94[2], cilow92[2], cilow90[2])*100
print(tab1)

Figure3 <- ggplot(tab1,aes(x = factor(Policy,level=c('0 TL','200 TL','400 TL','600 TL', '800 TL', '1000 TL')), y = ITT,ymin = Ci_lower, ymax = Ci_upper))
Figure3 + scale_colour_manual(values=c(values=c("tomato3", "turquoise4"))) + geom_point(position=position_dodge(width=0.8) ,size = 2, color="tomato3") + 
  geom_linerange(position=position_dodge(width=0.5), color="tomato3", size =0.5) +
  geom_hline(yintercept=0, colour="grey", linetype = "dashed", size = 1) +
  theme_bw() + coord_cartesian(ylim = c(-25, 20)) +  ylab("ITT in %-points") +xlab("Monetary difference in policy proposal between an LP and an HP candidate")
#ggsave("Figure3.png", dpi=600) 

#Figure 4 in the manuscript

#wide to long and long to wide -> for creating the variables for trust/competence scores
data_long <- data %>% 
  # same pivot_longer as before
  pivot_longer(matches("trust_|comp_"), 
               names_to = "attribute_pitch", 
               values_to = "trust_comp_score") %>% 
  separate(attribute_pitch, into = c("attribute", "pitch"), sep = "_", convert = TRUE)

data_long <- data_long %>% 
  pivot_wider(names_from = attribute, values_from = trust_comp_score)


data_long$isfemale<-NA
data_long$isfemale[data_long$female_candidate==1]<-1

data_long$nofemale<-NA
data_long$nofemale[data_long$female_candidate==0]<-1

data_long$trust2 <- rescale(data_long$trust)
data_long$comp2 <- rescale(data_long$comp)

sub_female<-subset(data_long, !is.na(isfemale))
sub_nofemale<-subset(data_long, !is.na(nofemale))

data_long$pitch <- as.factor(data_long$pitch)

full_coeftrust<-lm_robust(trust2 ~ pitch, data=data_long)$coefficients
full_cilowtrust<-lm_robust(trust2 ~ pitch, data=data_long)$conf.low
full_cihightrust<-lm_robust(trust2 ~ pitch, data=data_long)$conf.high

full_coefcomp<-lm_robust(comp2 ~ pitch, data=data_long)$coefficients
full_cilowcomp<-lm_robust(comp2 ~ pitch, data=data_long)$conf.low
full_cihighcomp<-lm_robust(comp2 ~ pitch, data=data_long)$conf.high

women_coeftrust<-lm_robust(trust2 ~ pitch, data=sub_female)$coefficients
women_cilowtrust<-lm_robust(trust2 ~ pitch, data=sub_female)$conf.low
women_cihightrust<-lm_robust(trust2 ~ pitch, data=sub_female)$conf.high

women_coefcomp<-lm_robust(comp2 ~ pitch, data=sub_female)$coefficients
women_cilowcomp<-lm_robust(comp2 ~ pitch, data=sub_female)$conf.low
women_cihighcomp<-lm_robust(comp2 ~ pitch, data=sub_female)$conf.high

nowomen_coeftrust<-lm_robust(trust2 ~ pitch, data=sub_nofemale)$coefficients
nowomen_cilowtrust<-lm_robust(trust2 ~ pitch, data=sub_nofemale)$conf.low
nowomen_cihightrust<-lm_robust(trust2 ~ pitch, data=sub_nofemale)$conf.high

nowomen_coefcomp<-lm_robust(comp2 ~ pitch, data=sub_nofemale)$coefficients
nowomen_cilowcomp<-lm_robust(comp2 ~ pitch, data=sub_nofemale)$conf.low
nowomen_cihighcomp<-lm_robust(comp2 ~ pitch, data=sub_nofemale)$conf.high

tab2<-NA
tab2 <- data.frame(c(rep("1. Full sample",2), rep("3. Woman candidate",2), rep("2. Man candidate",2)))
tab2$gender <- NA
tab2$ITT<- NA
tab2$Cilower <- NA
tab2$Ciupper <- NA
tab2$Ratings <- NA
colnames(tab2) <- c("CandidateGender", "ITT","Ci_lower", "Ci_upper","Ratings")
tab2$ITT<- c(full_coeftrust[2],full_coefcomp[2], women_coeftrust[2], women_coefcomp[2],nowomen_coeftrust[2],nowomen_coefcomp[2])*100
tab2$Ci_upper<- c(full_cihightrust[2],full_cihighcomp[2], women_cihightrust[2], women_cihighcomp[2], nowomen_cihightrust[2], nowomen_cihighcomp[2])*100
tab2$Ci_lower<- c(full_cilowtrust[2], full_cilowcomp[2],women_cilowtrust[2],women_cilowcomp[2],nowomen_cilowtrust[2],nowomen_cilowcomp[2])*100
tab2[1,5]<-"Trustworhiness ratings"
tab2[2,5]<-"Competence ratings"
tab2[3,5]<-"Trustworhiness ratings"
tab2[4,5]<-"Competence ratings"
tab2[5,5]<-"Trustworhiness ratings"
tab2[6,5]<-"Competence ratings"
print(tab2)

Figure4 <- ggplot(tab2, aes(x=CandidateGender, y=ITT, colour=Ratings)) +
  geom_errorbar(aes(ymin=Ci_lower, ymax=Ci_upper), width=.07, position=position_dodge(width=0.5)) +
  scale_colour_manual(values=c("tomato3", "turquoise4", "tomato3", "turquoise4","tomato3", "turquoise4")) +
  geom_line() + geom_point(aes(shape=Ratings), position=position_dodge(width=0.5)) + geom_hline(yintercept=0, colour="grey", linetype = "dashed", size = 1) +
  theme_bw() + ggtitle("ITT of voice pitch on trust/competence ratings") + coord_cartesian(ylim = c(-25,25)) +
  ylab("ITT in %-points")  + xlab("Candidate Gender")
print(Figure4)
#ggsave("Figure4.png", dpi=600)


##Figure 5 in the manuscript

#resp_gender=0 if the participant is female and resp_gender=1 if the participant is male
data$iswoman<-NA
data$iswoman[data$resp_gender==0]<-1

data$nowoman<-NA
data$nowoman[data$resp_gender==1]<-1

sub_women<-subset(data, !is.na(iswoman))
sub_nowomen<-subset(data, !is.na(nowoman))

full_coef<-lm_robust(scale10 ~ treatment, data=data)$coefficients
full_cilow<-lm_robust(scale10 ~ treatment, data=data)$conf.low
full_cihigh<-lm_robust(scale10 ~ treatment, data=data)$conf.high
coef98<-lm_robust(scale98 ~ treatment, data=data)$coefficients
cilow98<-lm_robust(scale98 ~ treatment, data=data)$conf.low
cihigh98<-lm_robust(scale98 ~ treatment, data=data)$conf.high

women_coef<-lm_robust(scale10 ~ treatment, data=sub_women)$coefficients
women_cilow<-lm_robust(scale10 ~ treatment, data=sub_women)$conf.low
women_cihigh<-lm_robust(scale10 ~ treatment, data=sub_women)$conf.high

nowomen_coef<-lm_robust(scale10 ~ treatment, data=sub_nowomen)$coefficients
nowomen_cilow<-lm_robust(scale10 ~ treatment, data=sub_nowomen)$conf.low
nowomen_cihigh<-lm_robust(scale10 ~ treatment, data=sub_nowomen)$conf.high

women_coef98<-lm_robust(scale98 ~ treatment, data=sub_women)$coefficients
women_cilow98<-lm_robust(scale98 ~ treatment, data=sub_women)$conf.low
women_cihigh98<-lm_robust(scale98 ~ treatment, data=sub_women)$conf.high

nowomen_coef98<-lm_robust(scale98 ~ treatment, data=sub_nowomen)$coefficients
nowomen_cilow98<-lm_robust(scale98 ~ treatment, data=sub_nowomen)$conf.low
nowomen_cihigh98<-lm_robust(scale98 ~ treatment, data=sub_nowomen)$conf.high

tab3<-NA
tab3 <- data.frame(c(rep("1. Full sample",2), rep("2. Women participants",2), rep("3. Men participants",2)))
tab3$gender <- NA
tab3$ITT<- NA
tab3$Cilower <- NA
tab3$Ciupper <- NA
tab3$Policy <- NA
colnames(tab3) <- c("RespondentGender", "ITT","Ci_lower", "Ci_upper","Policy")
tab3$ITT<- c(full_coef[2],coef98[2], women_coef[2], women_coef98[2],nowomen_coef[2],nowomen_coef98[2])*100
tab3$Ci_upper<- c(full_cihigh[2],cihigh98[2], women_cihigh[2], women_cihigh98[2], nowomen_cihigh[2], nowomen_cihigh98[2])*100
tab3$Ci_lower<- c(full_cilow[2], cilow98[2],women_cilow[2],women_cilow98[2], nowomen_cilow[2],nowomen_cilow98[2])*100
tab3[1,5]<-"0 TL"
tab3[2,5]<-"200 TL"
tab3[3,5]<-"0 TL"
tab3[4,5]<-"200 TL"
tab3[5,5]<-"0 TL"
tab3[6,5]<-"200 TL"
print(tab3)

Figure5 <- ggplot(tab3, aes(x=RespondentGender, y=ITT, colour=Policy)) +
  geom_errorbar(aes(ymin=Ci_lower, ymax=Ci_upper), width=.07, position=position_dodge(width=0.5)) +
  scale_colour_manual(values=c("tomato3", "turquoise4", "tomato3", "turquoise4","tomato3", "turquoise4")) +
  geom_line() + geom_point(aes(shape=Policy), position=position_dodge(width=0.5)) + geom_hline(yintercept=0, colour="grey", linetype = "dashed", size = 1) +
  theme_bw() + coord_cartesian(ylim = c(-35,35)) +
  ylab("ITT in %-points") +  xlab("Participant Gender")+theme(axis.text.x=element_text(size=10))
print(Figure5)
#ggsave("Figure5.png", width=10, height=5, dpi=600)


#APPENDIX------------------------------

#Table 1 Summary Statistics------------------------------------
library(arsenal)

data$treatment_balance <- NA
data$treatment_balance[data$female_candidate==1 & data$healthcare==1] <- 1
data$treatment_balance[data$female_candidate==1 & data$healthcare==0] <- 2
data$treatment_balance[data$female_candidate==0 & data$healthcare==1] <- 3
data$treatment_balance[data$female_candidate==0 & data$healthcare==0] <- 4
table(data$treatment_balance)

data$Gender <- "Other"
data$Gender[data$gender_identity==2] <- "Female"
data$Gender[data$gender_identity==1] <- "Male"

data$Ideology <- 1
data$Ideology[data$ideology<0.5] <- 0

data$Age <- 1
data$Age[22< data$age & data$age<25] <- 2
data$Age[25<=data$age] <- 3

data$Trust <- "Most people can be trusted"
data$Trust[data$gen_trust==1] <- "People should be approached carefully"

data$gender_role <- NA #"In general, do you think female politicians or male politicians are more successful in addressing education/healthcare-related issues?"
data$gender_role[data$gender_comp==1] <- "Women"
data$gender_role[data$gender_comp==2] <- "Men"
data$gender_role[data$gender_comp==3] <- "No difference"


data$educ_satisfaction_num <- NA
data$educ_satisfaction_num[data$ed_satisfaction==5] <- 1
data$educ_satisfaction_num[data$ed_satisfaction==4] <- 2
data$educ_satisfaction_num[data$ed_satisfaction==3] <- 3
data$educ_satisfaction_num[data$ed_satisfaction==2] <- 4
data$educ_satisfaction_num[data$ed_satisfaction==1] <- 5


data$hc_satisfaction_num <- NA
data$hc_satisfaction_num[data$hc_satisfaction==5] <- 1
data$hc_satisfaction_num[data$hc_satisfaction==4] <- 2
data$hc_satisfaction_num[data$hc_satisfaction==3] <- 3
data$hc_satisfaction_num[data$hc_satisfaction==2] <- 4
data$hc_satisfaction_num[data$hc_satisfaction==1] <- 5


#Satisfaction from healthcare/education 
data$Satisfaction <- ifelse(!is.na(data$educ_satisfaction_num),data$educ_satisfaction_num,data$hc_satisfaction_num)

df_desc = data %>% dplyr::select(Gender,age,Ideology,participation,Trust,Satisfaction,govt_provider,gender_role,treatment_balance)
summary(tableby(treatment_balance ~ ., data = df_desc), title = "Descriptive Statistics", text="latex")


#Table 2 Balance test------------------------------------
  
model_balance1<-lm_robust(treatment ~  resp_gender_num + factor(Age)+ income + participation + ideology + gen_trust , data=data)
summary(model_balance1)

texreg(list(model_balance1),include.ci = FALSE,
       custom.model.names = c("Treatment -Candidate Gender (Woman=0, Man=1)"),
       caption = "Balance table",  omit.coef = "factor",
       digits = 3)

#Figure A.2--------------------------------------
ggplot(data, aes(x=govt_provider)) + geom_histogram(binwidth = (0.1)) + facet_grid(~ healthcare)

#Figure A.3--------------------------------------
ggplot(data, aes(x=satisfaction)) + geom_histogram(binwidth = (0.8)) + facet_grid(~ healthcare)


##Table 3 in the Appendix-------------------------------

#creating a regression table with the adjusted and unadjusted ITTs
lm1 <- lm_robust(scale10 ~ treatment, data=data)
lm2 <- lm_robust(scale98 ~ treatment, data=data)
lm3 <- lm_robust(scale96 ~ treatment, data=data)
lm4 <- lm_robust(scale94 ~ treatment, data=data)
lm5 <- lm_robust(scale92 ~ treatment, data=data)
lm6 <- lm_robust(scale90 ~ treatment, data=data)

#covariate adjustment
lm1_adj <- lm_robust(scale10 ~ treatment + resp_gender_num + factor(Age)+ factor(income) + participation + Ideology + gen_trust, data=data)
lm2_adj <- lm_robust(scale98 ~ treatment + resp_gender_num + factor(Age)+ factor(income) + participation + Ideology + gen_trust, data=data)
lm3_adj <- lm_robust(scale96 ~ treatment + resp_gender_num + factor(Age)+ factor(income) + participation + Ideology + gen_trust, data=data)
lm4_adj <- lm_robust(scale94 ~ treatment + resp_gender_num + factor(Age)+ factor(income) + participation + Ideology + gen_trust, data=data)
lm5_adj <- lm_robust(scale92 ~ treatment + resp_gender_num + factor(Age)+ factor(income) + participation + Ideology + gen_trust, data=data)
lm6_adj <- lm_robust(scale90 ~ treatment + resp_gender_num + factor(Age)+ factor(income) + participation + Ideology + gen_trust, data=data)

latex_export<-texreg(list(lm1,lm1_adj,lm2,lm2_adj, lm3,lm3_adj,lm4,lm4_adj,lm5,lm5_adj,lm6,lm6_adj),stars=c(0.01, 0.05, 0.1),
                     include.ci=FALSE,custom.model.names=c("0 TL","0 TL","200 TL","200 TL","400 TL","400 TL","600 TL","600 TL","800 TL","800 TL","1000 TL","1000 TL"),
                     custom.gof.rows=list("Covariate adjusted" = c("No","Yes","No","Yes","No","Yes","No","Yes","No","Yes","No","Yes"))
                     , omit.coef =c('resp_gender_num|Age|income|participation|Ideology|gen_trust'), center=TRUE, 
                     caption= "Linear Regression of the Probability of Voting for the LP candidate on Treatment Assignment",booktabs=TRUE)
latex_export 

#Table 4 in the Appendix-----------------------------
table(data$sound_prob)
data_sound_prob <- subset(data, sound_prob==2)

lm1_s <- lm_robust(scale10 ~ treatment, data=data_sound_prob)
lm2_s <- lm_robust(scale98 ~ treatment, data=data_sound_prob)
lm3_s <- lm_robust(scale96 ~ treatment, data=data_sound_prob)
lm4_s <- lm_robust(scale94 ~ treatment, data=data_sound_prob)
lm5_s <- lm_robust(scale92 ~ treatment, data=data_sound_prob)
lm6_s <- lm_robust(scale90 ~ treatment, data=data_sound_prob)

lm1_adj_s <- lm_robust(scale10 ~ treatment + resp_gender_num + factor(Age)+ factor(income) + participation + Ideology + gen_trust, data=data_sound_prob)
lm2_adj_s <- lm_robust(scale98 ~ treatment + resp_gender_num + factor(Age)+ factor(income) + participation + Ideology + gen_trust, data=data_sound_prob)
lm3_adj_s <- lm_robust(scale96 ~ treatment + resp_gender_num + factor(Age)+ factor(income) + participation + Ideology + gen_trust, data=data_sound_prob)
lm4_adj_s <- lm_robust(scale94 ~ treatment + resp_gender_num + factor(Age)+ factor(income) + participation + Ideology + gen_trust, data=data_sound_prob)
lm5_adj_s <- lm_robust(scale92 ~ treatment + resp_gender_num + factor(Age)+ factor(income) + participation + Ideology + gen_trust, data=data_sound_prob)
lm6_adj_s <- lm_robust(scale90 ~ treatment + resp_gender_num + factor(Age)+ factor(income) + participation + Ideology + gen_trust, data=data_sound_prob)

latex_export_s<-texreg(list(lm1_s,lm1_adj_s,lm2_s,lm2_adj_s, lm3_s,lm3_adj_s,lm4_s,lm4_adj_s,lm5_s,lm5_adj_s,lm6_s,lm6_adj_s),stars=c(0.01, 0.05, 0.1),
                       include.ci=FALSE,custom.model.names=c("0 TL","0 TL","200 TL","200 TL","400 TL","400 TL","600 TL","600 TL","800 TL","800 TL","1000 TL","1000 TL"),
                       custom.gof.rows=list("Covariate adjusted" = c("No","Yes","No","Yes","No","Yes","No","Yes","No","Yes","No","Yes"))
                       , omit.coef =c('resp_gender_num|Age|income|participation|Ideology|gen_trust'), center=TRUE, 
                       caption= "Linear Regression of the Probability of Voting for the LP candidate on Treatment Assignment excluding respondents experienced any type of trouble listening the experimental stimuli",booktabs=TRUE)
latex_export_s 


#Table 5 in the Appendix
table(data$UserAgent)
data$device <- str_extract(data$UserAgent, "iPhone|Linux|Macintosh|Windows")

data$device_computer <- 0
data$device_computer[data$device=="Macintosh" | data$device=="Windows"] <- 1


lm1_adj <- lm_robust(scale10 ~ treatment + device_computer+ duration+ speakers + resp_gender_num + factor(Age)+ factor(income) + participation + Ideology + gen_trust, data=data)
lm2_adj <- lm_robust(scale98 ~ treatment + device_computer+ duration+ speakers + resp_gender_num + factor(Age)+ factor(income) + participation + Ideology + gen_trust, data=data)
lm3_adj <- lm_robust(scale96 ~ treatment + device_computer+ duration+ speakers+ resp_gender_num + factor(Age)+ factor(income) + participation + Ideology + gen_trust, data=data)
lm4_adj <- lm_robust(scale94 ~ treatment + device_computer+ duration+ speakers+ resp_gender_num + factor(Age)+ factor(income) + participation + Ideology + gen_trust, data=data)
lm5_adj <- lm_robust(scale92 ~ treatment + device_computer+ duration+ speakers+ resp_gender_num + factor(Age)+ factor(income) + participation + Ideology + gen_trust, data=data)
lm6_adj <- lm_robust(scale90 ~ treatment + device_computer+ duration+ speakers+ resp_gender_num + factor(Age)+ factor(income) + participation + Ideology + gen_trust, data=data)

latex_export2<-texreg(list(lm1_adj,lm2_adj,lm3_adj,lm4_adj,lm5_adj,lm6_adj),stars=c(0.01, 0.05, 0.1),
                      include.ci=FALSE,custom.model.names=c("0 TL","200 TL","400 TL","600 TL","800 TL","1000 TL"),
                      custom.gof.rows=list("Covariate adjusted" = c("Yes","Yes","Yes","Yes","Yes","Yes"))
                      , omit.coef =c('device_computer|duration|speakers|resp_gender_num|Age|income|participation|Ideology|gen_trust'), center=TRUE, 
                      caption= "Linear Regression of the Probability of Voting for the LP candidate on Treatment Assignment",booktabs=TRUE)
latex_export2 

#Appendix A.3 Validation of the perceived voice pitch difference
data_val <- read.csv("validation_test_data.csv")
data_val_long <- gather(data_val,name, rating, Woman1:man3, factor_key=TRUE)
mean(data_val_long$rating)

data_val_long$candidate_gender <- 0
data_val_long$candidate_gender[data_val_long$name=="Woman1"] <- 1
data_val_long$candidate_gender[data_val_long$name=="Woman2"] <- 1
data_val_long$candidate_gender[data_val_long$name=="woman3"] <- 1
table(data_val_long$candidate_gender)

options(scipen=99)
t.test(data_val_long$rating, mu = 0, alternative="greater") #one-sided t-test
t.test(rating ~ candidate_gender, data = data_val_long, paired = TRUE) #paired t-test



